#include "newlock.h"
#include "ui_newlock.h"
#include "editlock.h"

newlock::newlock(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::newlock)
{
    ui->setupUi(this);
}

newlock::~newlock()
{
    delete ui;
}

void newlock::on_backButton_clicked()
{
    this->close();
    emit back_signal();
}

void newlock::on_confirmButton_2_clicked()
{
    QString path = ui->path->text();
#ifdef Q_OS_LINUX
    path = path[path.size()-1]==L'/' ? path : path+"/";
#endif
#ifdef Q_OS_WIN
    path = path[path.size()-1]==L'\\' ? path : path+"\";
#endif
    QString password = ui->password->text();
    if (path.isEmpty())
    {
        QMessageBox::warning(this,"test","路径不能为空",QMessageBox::Ok,QMessageBox::Cancel);
    }
    if (password.isEmpty())
    {
        QMessageBox::warning(this,"test","密码不能为空",QMessageBox::Ok,QMessageBox::Cancel);
    }
    //创建文件夹
    QDir dir(path);
    if (!dir.exists())
    {
        if (!dir.mkpath(path))
        {
            QMessageBox::warning(this,"test","创建文件夹失败",QMessageBox::Ok,QMessageBox::Cancel);
        }
    }
    //写入path文件
    QFile file("path.txt");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))return;
    QTextStream out(&file);
    out << path;
    file.close();
    //跳转下一界面
    this->hide();
    editlock *p_editlock = new editlock(password, path);
    connect(p_editlock, SIGNAL(back_newlock_signal()), this, SLOT(reshow_newlock()));
    p_editlock->show();
}

void newlock::reshow_newlock()
{
    this->show();
}
